package com.learning.jquizenglish.screen;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.JQuizEnglishFragment;
import com.learning.jquizenglish.common.adapter.BaseQuizAnswerAdapter;
import com.learning.jquizenglish.model.AnswerModel;
import com.learning.jquizenglish.model.DoQuizSession;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.widget.ViewHolder;

public class QuestionFragment extends JQuizEnglishFragment{

	private TextView txtQuestionNumber;
	private TextView txtQuestionContent;
	private ListView listAnswer;
	private int mQuestionNumber;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.question_page, container, false);
		txtQuestionNumber = (TextView) v.findViewById(R.id.txtQuestionNumber);
		txtQuestionContent = (TextView) v.findViewById(R.id.txtQuestion);
		listAnswer		= (ListView) v.findViewById(R.id.lvChoice);
		createQuestionData();
		return v;
	}

	public void createQuestionData(){
		final QuestionModel questionModel = DoQuizSession.getInstance().getListQuestions().get(mQuestionNumber);
		txtQuestionNumber.setText("Question "+(mQuestionNumber+1)+":");
		txtQuestionContent.setText(questionModel.title);
		final AnswerAdapter adapter = new AnswerAdapter(getActivity(), questionModel.answers);
		boolean isAnswered = questionModel.isAnswered;
		adapter.isAnswered = isAnswered;
		listAnswer.setAdapter(adapter);
		listAnswer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				questionModel.isAnswered = true;
				questionModel.isAnswerCorrect = adapter.checkAnswer(pos);
				DoQuizSession.getInstance().getListQuestions().set(mQuestionNumber, questionModel);
				adapter.chooseAnswer(pos);
				listAnswer.setEnabled(false);
			}
		});
		if(isAnswered){
			listAnswer.setEnabled(false);
		}
	}
	
	public void setQuestionNumber(int questionNumber){
		mQuestionNumber = questionNumber;
	}
	
	public static QuestionFragment create(int questionNumber){
		QuestionFragment questionFragment = new QuestionFragment();
		questionFragment.setQuestionNumber(questionNumber);
		return questionFragment;
	}
	
	public class AnswerAdapter extends BaseQuizAnswerAdapter{

		public Integer[] listIconAnswer = {R.drawable.choice_a,R.drawable.choice_b,R.drawable.choice_c,
												R.drawable.choice_d,R.drawable.choice_e};
		public Integer[] listIconAnswerChoise = {R.drawable.choice_al,R.drawable.choice_bl,R.drawable.choice_cl,
												R.drawable.choice_dl,R.drawable.choice_el};
		
		public AnswerAdapter(Activity activity, ArrayList<AnswerModel> datas) {
			super(activity, datas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if (row == null) {
				row = LayoutInflater.from(mActivity).inflate(R.layout.do_quiz_answer_item, null);
			}
			ImageView imgIcon = ViewHolder.get(row, R.id.imgIcon);
			TextView tvAnswer = ViewHolder.get(row, R.id.tvAnswer);
			TextView tvCheckAnswer = ViewHolder.get(row, R.id.tvCheckAnswer);
			AnswerModel model = getItem(position);
			if (model!=null) {
				tvAnswer.setText(model.title);
				if(isAnswered){
					if (model.isChoise) {
						imgIcon.setImageResource(listIconAnswerChoise[position]);
						tvCheckAnswer.setVisibility(View.VISIBLE);
						if (model.is_correct.trim().equals("1")) {
							tvCheckAnswer.setText("(Correct)");
							tvCheckAnswer.setTextColor(getResources().getColor(R.color.mau_xanhlo));
						}else{
							tvCheckAnswer.setText("(Wrong)");
							tvCheckAnswer.setTextColor(getResources().getColor(R.color.mau_gold));
						}
					}else{
						imgIcon.setImageResource(listIconAnswer[position]);
						if (model.is_correct.trim().equals("1")) {
							tvCheckAnswer.setVisibility(View.VISIBLE);
							tvCheckAnswer.setText("(Correct)");
							tvCheckAnswer.setTextColor(getResources().getColor(R.color.mau_xanhlo));
						}else{
							tvCheckAnswer.setVisibility(View.INVISIBLE);
						}
					}
				}else{
					imgIcon.setImageResource(listIconAnswer[position]);
					tvCheckAnswer.setVisibility(View.INVISIBLE);
				}
			}
			return row;
		}
		
	}
}
